package com.xpgk.mvc.productSet;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;

@Service(name = ProductSetService.serviceName)
public class ProductSetService extends BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(ProductSetService.class);

	public static final String serviceName = "productSetService";
	
	/**
	 * 添加产品
	 * @param orgid 
	 * @param uid 
	 * @param user
	 * @param organIds
	 * @return
	 */
	public RenderBean addProduct(Product product, List<ProductExperience> pes, String uid, String orgid){
		RenderBean result = new RenderBean();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_proBh);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		Product oProduct = Product.dao.findFirst(sql, product.getStr(Product.column_proBh));
		if(null == oProduct){
			product.set(Product.column_addtime, new Date());
			product.set(Product.column_createId, uid);
			product.set(Product.column_createDate, new Date());
			product.set(Product.column_orgId,orgid);
			product.save();
			//产品类型为“瘦身”添加使用精护使用次数
			if(product.get(Product.column_typeIds).equals("a07f80b6d9064a2e8357f0f87d21a115")&&pes.size() > 0){
				for(ProductExperience p : pes){
					p.set(ProductExperience.column_orgId,orgid);
					p.set(ProductExperience.column_productIds, product.getPKValue()).save();
				}
			}
			result.setCode(ConstantRender.render_success_code);
		}else{
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("产品编号重复！");
		}
		return result;
	}
	
	/**
	 * 编辑产品
	 * @param user
	 * @param organIds
	 * @return
	 */
	public RenderBean upProduct(Product product, List<ProductExperience> pes, String uid, String orgid){
		RenderBean result = new RenderBean();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_proBh);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		Product oProduct = Product.dao.findFirst(sql, product.getStr(Product.column_proBh));
		if(null == oProduct){
			product.set(Product.column_updateId, uid);
			product.set(Product.column_updateDate, new Date());
			product.set(Product.column_orgId,orgid);
			product.update();
			if(pes.size() > 0){
				for(ProductExperience p : pes){
					if(p.getPKValue() == null){
						p.set(ProductExperience.column_orgId,orgid);
						p.set(ProductExperience.column_productIds, product.getPKValue()).save();
					}else{
						p.set(ProductExperience.column_orgId,orgid);
						p.update();
					}
				}
			}
			result.setCode(ConstantRender.render_success_code);
		}else{
			if(product.getPKValue().equals(oProduct.getPKValue())){
				product.update();
				if(pes.size() > 0){
					for(ProductExperience p : pes){
						if(p.getPKValue() == null){
							p.set(ProductExperience.column_orgId,orgid);
							p.set(ProductExperience.column_productIds, product.getPKValue()).save();
						}else{
							p.set(ProductExperience.column_orgId,orgid);
							p.update();
						}
					}
				}
				result.setCode(ConstantRender.render_success_code);
			}else{
				result.setCode(ConstantRender.render_error_code);
				result.setMsg("产品编号重复！");
			}
		}
		return result;
	}

}
